home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 8 / FM Towns Free Software Collection 8.iso / t_os / townsmsx / scc2tif.c < prev    next >
Text File  |  1994-06-01  |  13KB  |  562 lines

  1. /*********************************************************************
  2.  
  3.            MSX SCREEN12 BSAVE FILE -> FM-TOWNS TIFF FILE
  4.                 by ちにゃと    (NIFTY-Serve ID:GFH01000)
  5.  
  6.                     1993/12/07       Ver 2.1 L20           
  7.  
  8. ********************************************************************/
  9.  
  10.                      
  11. #include <io.h>
  12. #include <fcntl.h>
  13. #include <stat.h>
  14. #include <memory.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <conio.h>
  19. #include <dos.h>
  20. #include <mos.h>
  21. #include <math.h>
  22. #include <stat.h>
  23.  
  24. /* TIFF HEADER
  25. +0   +1   +2   +3   +4   +5   +6   +7   +8   +9   +A   +B   +C   +D   +E   +F*/
  26. char tifheader[512] = {
  27. 0x49,0x49,0x2A,0x00,
  28. 0x08,0x00,0x00,0x00,
  29. 0x0F,0x00,
  30. 0xFE,0x00,0x04,0x00,0x01,0x00,0x00,0x00,
  31. 0x00,0x00,0x00,0x00,
  32. 0x00,0x01,0x03,0x00,0x01,0x00,0x00,0x00,
  33. 0x00,0x01,0x00,0x00,
  34. 0x01,0x01,0x03,0x00,0x01,0x00,0x00,0x00,
  35. 0xD4,0x00,0x00,0x00,
  36. 0x02,0x01,0x03,0x00,0x01,0x00,0x00,0x00,
  37. 0x10,0x00,0x00,0x00,
  38. 0x03,0x01,0x03,0x00,0x01,0x00,0x00,0x00,
  39. 0x01,0x00,0x00,0x00,
  40. 0x06,0x01,0x03,0x00,0x01,0x00,0x00,0x00,
  41. 0x01,0x00,0x00,0x00,
  42. 0x0A,0x01,0x03,0x00,0x01,0x00,0x00,0x00,
  43. 0x01,0x00,0x00,0x00,
  44. 0x11,0x01,0x04,0x00,0x01,0x00,0x00,0x00,
  45. 0x00,0x02,0x00,0x00,
  46. 0x15,0x01,0x03,0x00,0x01,0x00,0x00,0x00,
  47. 0x01,0x00,0x00,0x00,
  48. 0x16,0x01,0x04,0x00,0x01,0x00,0x00,0x00,
  49. 0xD4,0x00,0x00,0x00,
  50. 0x17,0x01,0x04,0x00,0x01,0x00,0x00,0x00,
  51. 0x00,0xAA,0x01,0x00,
  52. 0x19,0x01,0x03,0x00,0x01,0x00,0x00,0x00,
  53. 0xFF,0x7F,0x00,0x00,
  54. 0x1A,0x01,0x05,0x00,0x01,0x00,0x00,0x00,
  55. 0xF0,0x01,0x00,0x00,
  56. 0x1B,0x01,0x05,0x00,0x01,0x00,0x00,0x00,
  57. 0xF8,0x01,0x00,0x00,
  58. 0x1C,0x01,0x03,0x00,0x01,0x00,0x00,0x00,
  59. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  60. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  61. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  62. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  63. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  64. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  65. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  66. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  67. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  68. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  69. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  70. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  71. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  72. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  73. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  74. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  75. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  76. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  77. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  78. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  79. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  80. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  81. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  82. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  83. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  84. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  85. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  86. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  87. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  88. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  89. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  90. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  91. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  92. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  93. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  94. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  95. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  96. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  97. 0x00,0x00,0x00,0x00,0x00,0x00,
  98. 0x4B,0x00,0x00,0x00,0x02,0x00,0x00,0x00,
  99. 0x4B,0x00,0x00,0x00,0x02,0x00,0x00,0x00
  100. };
  101.  
  102. /* T-BIOS C ライブラリ ヘッダー */
  103. #include <fmcfrb.h>
  104. char fname[256];
  105. char filepath[256];
  106. char sccext[10];
  107. char srsext[10];
  108. char scchead[10];
  109. char sccdata[256*64][4];
  110. char tifdata[256*64][8];
  111. char wildfname[100][256];
  112. unsigned char r[4];
  113. unsigned char g[4];
  114. unsigned char b[4];
  115. unsigned char y[4];
  116. unsigned char k,kj;
  117. int bb[4];
  118. int rshift,gshift,bshift;
  119. int sccsize;
  120. int i,j,wildcard;
  121. int w_ret;
  122. int mode;
  123. int fileno;
  124. int namelen;
  125. int flong;
  126. int findflag;
  127. int wildct;
  128. static struct find_t wildfbuf;
  129. static unsigned long ret,pan;
  130.  
  131. /************************************************************
  132. 【オプション解析】
  133.  
  134.         概要    オプションパラメ-タの解析を行い結果を返す
  135.         用法    optionset( argc, *argv[] );
  136.         引数    int        argc
  137.                 char    *argv[]
  138.         戻り値     int        0    正常終了
  139.                         -1    オプションの指定が正しくない
  140. ************************************************************/
  141.  
  142. int optionset( int argc, char *argv[] ){
  143.  
  144.     int        count;
  145.     char    c;
  146.     ret=0;
  147.  
  148.     strcpy(sccext,"SCC");
  149.     strcpy(srsext,"SRS");
  150.     rshift=0;
  151.     gshift=0;
  152.     bshift=0;    
  153.     ret = 0;
  154.     findflag=0;
  155.     /* オプション パラメータ 解析 */
  156.     if ( argc == 1 ) {
  157.         ret=-1;
  158.     } 
  159.     else{
  160.         for ( count = 1; count < argc; count++ ){
  161.             c = *argv[count];                        /* 先頭文字 get */
  162.             if ( c == '/' || c == '-' ){
  163.                 c = *(argv[count] + 1);                /* 2 文字目 get */
  164.                 switch ( c ){
  165.                     case 'h':
  166.                     case 'H':
  167.                     case '?':
  168.                         ret = -1;
  169.                         break;
  170.  
  171.                     case 'r':
  172.                     case 'R':
  173.                         rshift = *(argv[count] + 2);
  174.                         rshift -= 0x30; 
  175.                         if ((*(argv[count] + 3)>0x30) &&
  176.                                 (*(argv[count] + 3)<0x34)   ){
  177.                             rshift=rshift*10+(*(argv[count] + 3))-0x30;
  178.                         }
  179.                         break;
  180.  
  181.                     case 'g':
  182.                     case 'G':
  183.                         gshift = *(argv[count] + 2);
  184.                         gshift-=0x30;
  185.                         if ((*(argv[count] + 3)>0x30) &&
  186.                                 (*(argv[count] + 3)<0x34)   ){
  187.                             gshift=gshift*10+(*(argv[count] + 3))-0x30;
  188.                         }
  189.                         break;
  190.  
  191.                     case 'b':
  192.                     case 'B':
  193.                         bshift = *(argv[count] + 2);
  194.                         bshift-=0x30;
  195.                         if ((*(argv[count] + 3)>0x30) &&
  196.                                 (*(argv[count] + 3)<0x34)   ){
  197.                             bshift=bshift*10+(*(argv[count] + 3))-0x30;
  198.                         }
  199.                         break;
  200.  
  201.                       /* 無効 パラメータ */
  202.                     default:
  203.                     ret = -1;
  204.                 }        /* <switch> */
  205.             }
  206.             else{
  207.                    strcpy(fname, argv[count] );
  208.              }
  209.         }    /* <for> */
  210.       }
  211.     if(ret==-1){
  212.         printf("****************************************************\n");
  213.         printf("   MSX SCREEN12 BSAVE FILE -> FM-TOWNS TIFF FILE\n");
  214.         printf("        by ちにゃと    (NIFTY-Serve ID:GFH01000)\n");
  215.         printf("            1993/12/07       Ver 2.1 L20        \n");
  216.         printf("****************************************************\n");
  217.         printf("\n");
  218.         printf("用法) RUN386  SCC2TIF.EXP [-Rx] [-Gx] [-Bx] FILENAME\n");
  219.         printf("\n");
  220.         printf("-Rx -Gx -Bx   RGBの強さを調整  x=0~31 \n");
  221.         printf("              省略時は -R0 -G0 -B0\n");
  222.         printf("              \n");
  223.         printf("\n");
  224.         printf("FILENAME      拡張子が .SCC .SRS  のみ\n");
  225.         printf("              ワイルドカ-ドも指定可能\n");
  226.         printf("\n");
  227.         printf("ex1)  RUN386 SCC2TIF.EXP SAMPLE.SCC\n");
  228.         printf("ex2)  RUN386 SCC2TIF.EXP A:*.SCC\n");
  229.         printf("ex3)  RUN386 SCC2TIF.EXP A:\\SCC\\SAMPLE*.SCC\n");
  230.         printf("\n");
  231.         printf("\n");
  232.         printf("\n");
  233.         return(-1);
  234.     }
  235.     namelen=strlen(fname);
  236.     flong=namelen;
  237.     for ( i =namelen-1; (i+1)>0 ;i-- ){
  238.         fname[i]=toupper(fname[i]);
  239.     }
  240.     for ( i =namelen-1; (i+1)>0 ;i-- ){
  241.         if ( fname[i] != 0x5c ){
  242.             flong -= 1;
  243.         }
  244.         else {
  245.             findflag=1;
  246.             break;
  247.         }
  248.     }
  249.     if(findflag==0){
  250.         flong=1;
  251.         for ( i = 0; i <255 ;i ++ ){
  252.             if ( fname[i] != 0x3a ){
  253.                 flong += 1;
  254.             }
  255.             else {
  256.                 findflag=2;
  257.                 break;
  258.             }
  259.         }
  260.     }
  261.     if(findflag!=0){
  262.         strncpy( filepath,fname,flong );
  263.     }
  264.     flong =strlen(fname);
  265.     for (i=0;i<3;i++){
  266.         if(fname[flong-3+i]!=sccext[i]&&fname[flong-3+i]!=(sccext[i]+0x20)&&
  267.            fname[flong-3+i]!=srsext[i]&&fname[flong-3+i]!=(srsext[i]+0x20) )
  268.         {
  269.             printf("拡張子が .SCC でも .SRS でもありません。\n");
  270.             return(-1);
  271.         }
  272.     }
  273.     return ret;
  274. }
  275. /************************************************************
  276.  
  277.                         sccload
  278.  
  279. ************************************************************/
  280. int sccload()
  281.  
  282. {
  283. char fname2[256];
  284.  
  285. strcpy(fname2,fname);
  286. printf("%s  ",fname);
  287.  
  288.  
  289.     if (_access(fname2,0) !=0 ){
  290.         printf("no scc file");
  291.         return(-1);
  292.     }
  293.     else{
  294.         pan=_open(fname2,_O_BINARY | _O_RDWR );
  295.         if (pan== -1 ){
  296.             return(-1);
  297.         }
  298.         ret=_read(pan,scchead,7);
  299.         if (ret== -1){
  300.             _close(pan);
  301.             return(-1);
  302.         }
  303.         else{
  304.             if(scchead[0]!=0xfe){
  305.                 printf("BSAVE FORMATではありません。\n");
  306.                 _close(pan);
  307.                 return(-1);
  308.             }
  309.             sccsize=scchead[4]*256+scchead[3]+1;
  310.             for( ; ; ){
  311.                 ret = _read(pan,&sccdata[0][0],256*250);
  312.                 if (ret == -1 ){
  313.                     _close(pan);
  314.                     printf("読み込みエラ-が発生しました。\n");
  315.                     return(-1);
  316.                 }
  317.                 else{
  318.                     _close(pan);
  319.                     break;
  320.                 }
  321.             }
  322.         }
  323.     }
  324.     return(0);
  325. }
  326. /***************************************************************
  327. 【デ-タファイルを作成する】
  328.  
  329.         概要    
  330.         用法    tifsave();
  331.         引数    無し
  332. ***************************************************************/
  333.  
  334. int tifsave ( void )
  335. {
  336.     char fname2[256];
  337.     int        wsize;
  338.     flong = 0;
  339.  
  340.     for ( i = 0; i <251 ;i ++ ){
  341.         if ( fname[i] != 0x2e ){
  342.             flong += 1;
  343.         }
  344.         else {
  345.             break;
  346.         }
  347.     }
  348.     strncpy( fname2,fname,flong );
  349.     fname2[flong]=0x00;
  350.     strcat( fname2,".TIF");
  351.     fname2[flong+4]=0x00;
  352.     tifheader[0x2a]=sccsize/256;
  353.     tifheader[0x7e]=sccsize/256;
  354.  
  355.  
  356.     if ( _access( fname2, 0 ) == 0 ) {
  357.         printf( " 同名のファイルがあります。上書きします。" );
  358.         ret = _unlink ( fname2 );
  359.     }
  360.     ret = 0;
  361.     pan = _open( fname2,_O_BINARY | _O_RDWR | _O_CREAT, _S_IREAD | _S_IWRITE );
  362.     if ( pan == -1) {
  363.         printf("  書き込みのオ-プンに失敗しました。 \n" );
  364.         ret = -1;
  365.     }
  366.     if ( ret == 0 ) {
  367.         wsize = _write( pan, &tifheader[0], 512  );
  368.         if ( wsize == -1 || wsize != 512 ) {
  369.             printf("  ヘッダ書き込みに失敗しました。\n" );
  370.             _close( pan );
  371.             return(-1);
  372.         }
  373.         else{
  374.  
  375.             wsize = _write( pan, &tifdata[0][0], sccsize*2  );
  376.             if ( wsize == -1 || wsize != ( sccsize*2 ) ) {
  377.                 printf("  デ-タ書き込みに失敗しました。\n" );
  378.                 _close( pan );
  379.                 return(-1);
  380.             }
  381.             else{
  382.                 printf("\n");
  383.             }
  384.         }
  385.     }
  386.     return (0);
  387. }
  388.  
  389.  
  390. /*********************************************************
  391.  
  392.                           convert
  393.                           
  394. *********************************************************/
  395. int convert(){
  396.  
  397.     for(i=0;i<sccsize/4;i++){
  398.         y[0]=sccdata[i][0] / 8;
  399.         y[1]=sccdata[i][1] / 8;
  400.         y[2]=sccdata[i][2] / 8;
  401.         y[3]=sccdata[i][3] / 8;
  402.         k = ( sccdata[i][0] & 7 )+ (sccdata[i][1] & 3 )*8;
  403.         kj= ( sccdata[i][2] & 7 )+ (sccdata[i][3] & 3 )*8;
  404.         for(j=0;j<4;j++){
  405.             bb[j]=y[j];
  406.             r[j]=y[j];
  407.             g[j]=y[j];
  408.             b[j]=y[j];
  409.             if((sccdata[i][1] & 4 )==0){
  410.                 g[j]+=k;
  411.                 bb[j]-=k;
  412.             }
  413.             else{
  414.                 g[j]-=(32-k);
  415.                 bb[j]+=(32-k);
  416.             }
  417.             if((sccdata[i][3] & 4 )==0){
  418.                 r[j]+=kj;
  419.                 bb[j]-=kj*2;
  420.             }
  421.             else{
  422.                 r[j]-=(32-kj);
  423.                 bb[j]+=(32-kj)*2;
  424.             }
  425.             if( bb[j] >= 0 ){
  426.                 b[j]+=(bb[j]/4);
  427.             }
  428.             else{
  429.                 if( b[j] > (-1)*((bb[j]+2)/4) ){
  430.                     b[j]+=(bb[j]/4);
  431.                 }
  432.                 else{
  433.                     b[j]=0;
  434.                 }
  435.             }
  436.             r[j]+=rshift;
  437.             g[j]+=gshift;
  438.             b[j]+=bshift;
  439.             if ((r[j] & 0x80)!=0){
  440.                 r[j]=0;
  441.             }
  442.             if ((g[j] & 0x80)!=0){
  443.                 g[j]=0;
  444.             }
  445.             if ((b[j] & 0x80)!=0){
  446.                 b[j]=0;
  447.             }
  448.             if (r[j]>31){
  449.                 r[j]=31;
  450.             }
  451.             if (g[j]>31){
  452.                 g[j]=31;
  453.             }
  454.             if (b[j]>31){
  455.                 b[j]=31;
  456.             }
  457.             tifdata[i][j*2+0]=(g[j]*1024+r[j]*32+b[j])%256;
  458.             tifdata[i][j*2+1]=(g[j]*1024+r[j]*32+b[j])/256;
  459.         }
  460.     }
  461.     return(0);
  462. }
  463.  
  464. /*********************************************************
  465.  
  466.                           メイン
  467.  
  468. *********************************************************/
  469.  
  470.  
  471. void main( int argc, char *argv[] ){
  472.     fileno=0;
  473.     mode=0x16;
  474.     if(optionset( argc, argv )!=0){
  475.         exit(-1);
  476.     }
  477.     for ( i = 0; i <251 ;i ++ ){
  478.         if ( fname[i] != 0x2a ){
  479.             flong += 1;
  480.         }
  481.         else {
  482.             wildcard=1;
  483.             break;
  484.         }
  485.     }
  486.     if(wildcard==1){
  487.         w_ret=_dos_findfirst((char*)fname,mode,&wildfbuf);
  488.         if(w_ret){
  489.             printf("指定のワイルドカ-ドが見つかりません\n");
  490.             exit(-1);
  491.         }
  492.         else{
  493.             strcpy(wildfname[fileno],wildfbuf.name);
  494.             fileno++;
  495.             for (;;){
  496.                 w_ret=_dos_findnext(&wildfbuf);
  497.                 if(w_ret){
  498.                     break;
  499.                 }
  500.                 strcpy(wildfname[fileno],wildfbuf.name);
  501.                 fileno++;
  502.             }
  503.         }
  504.         for(wildct=0;wildct<fileno;wildct++){
  505.             strcpy(fname,filepath);
  506.             strcat(fname,wildfname[wildct]);
  507.              if(sccload()!=0){
  508.                 exit(-1);
  509.             }
  510.             if(convert()!=0){
  511.                 exit(-1);
  512.             }
  513.             strcpy(fname,wildfname[wildct]);
  514.             if(tifsave()!=0){
  515.                 exit(-1);
  516.             }
  517.         }
  518.     }
  519.     else{
  520.         if(sccload()!=0){
  521.             exit(-1);
  522.         }
  523.         if(convert()!=0){
  524.             exit(-1);
  525.         }
  526.         findflag=0;
  527.         namelen=strlen(fname);
  528.         flong=namelen;
  529.         for ( i =namelen-1; (i+1)>0 ;i-- ){
  530.             if ( fname[i] != 0x5c ){
  531.                 flong -= 1;
  532.             }
  533.             else {
  534.                 findflag=1;
  535.                 break;
  536.             }
  537.         }
  538.         if(findflag==0){
  539.             flong=1;
  540.             for ( i = 0; i <255 ;i ++ ){
  541.                 if ( fname[i] != 0x3a ){
  542.                     flong += 1;
  543.                 }
  544.                 else {
  545.                     findflag=2;
  546.                     break;
  547.                 }
  548.             }
  549.         }
  550.         if(findflag!=0){
  551.             for(i=flong;i<namelen;i++){
  552.                 fname[i-flong]=fname[i];
  553.             }
  554.             fname[i-flong]=0;
  555.         }
  556.         if(tifsave()!=0){
  557.             exit(-1);
  558.         }
  559.     }
  560.     exit(0);
  561. }
  562.